#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include <SDL/SDL.h>
#include "SDL/SDL_Image.h"
#include "PNG_GameEntities.h"

void update_game_state (ball *B, int deltatime){
    B->x+=B->vx*deltatime/1000.0;
    B->y+=B->vy*deltatime/1000.0;
    if(B->x<0){
        B->x=-1*B->x;
        B->vx=-1*B->vx;
    }
    if(B->x>1280){
        B->x=2560-B->x;
        B->vx=-1*B->vx;
    }
    if(B->y<0){
        B->y=-1*B->y;
        B->vy=-1*B->vy;
    }
    if(B->y>720){
        B->y=1440-B->y;
        B->vy=-1*B->vy;
    }
}

void draw_game (ball B, SDL_Surface *screen){
    SDL_Rect src;
    src.x=0;
    src.y=0;
    src.h=10;
    src.w=10;
    SDL_Rect dest;
    dest.x=B.x-5;
    dest.y=B.y-5;
    dest.w=10;
    dest.h=10;
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
    SDL_BlitSurface(B.sprite,&src,screen,&dest);
}

char PNG_Game(SDL_Surface *screen){
    char mode='n';
    int ticktime;
    ball testB;
    testB.x=640;
    testB.y=360;
    testB.vx=200;
    testB.vy=400;
    testB.sprite=IMG_Load("images/ball.png");
    ticktime=SDL_GetTicks();
    while(mode=='n'){
        SDL_Event event;
        while (SDL_PollEvent(&event)){
                switch (event.type){
                case SDL_QUIT:
                    mode='m';
                break;
                case SDL_KEYDOWN:{
                    switch (event.key.keysym.sym){
                        case SDLK_ESCAPE:
                            printf("escape");
                            mode='m';
                        break;
                        default:
                        break;
                    }
                }
            }
        }
        update_game_state(&testB,SDL_GetTicks()-ticktime);
        draw_game(testB,screen);
        ticktime=SDL_GetTicks();
        SDL_Flip(screen);
    }
    return mode;
}
